//
//  HZMOtherShareViewController.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/10/22.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMOtherShareViewController.h"
#import "HZMItemsDetailsVC.h"

@interface HZMOtherShareViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain) NSMutableArray *itemArr;
@property(nonatomic, retain) UICollectionView *collectionView;
@property(nonatomic, retain) HZMDataModel *myData;

@end


@implementation HZMOtherShareViewController

- (void)dealloc
{
    [_itemArr release];
    [_collectionView release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self createNetWork];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createCollectionView];
    
    [self collectionView];
    
    [self createData];
    
//    [self createNetWork];
}



#pragma mark CollectionView Setting
- (void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake((WIDTH - 20) / 2, 2 * HEIGHT / 5);
    flowLayout.minimumInteritemSpacing = 2;
    flowLayout.minimumLineSpacing = 2;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 44) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:241 green:254 blue:247 alpha:1];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    [self.collectionView registerClass:[HZMHotPageCVC class] forCellWithReuseIdentifier:@"reuse"];
    
}

#pragma mark CollectionView Delegate Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myData setValuesForKeysWithDictionary:[self.itemArr[indexPath.row] hotData]];
    HZMHotPageCVC *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[self.myData cover_image_url]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
    cell.nameLabel.text = [self.myData name];
    cell.priceLabel.text = [NSString stringWithFormat:@"🐑%@", [self.myData price]];
    cell.favoriteLabel.text = [NSString stringWithFormat:@"%@", [self.myData favorites_count]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myData setValuesForKeysWithDictionary:[self.itemArr[indexPath.row] hotData]];
    HZMItemsDetailsVC *itemsVC = [[HZMItemsDetailsVC alloc] init];
    itemsVC.keyId = [self.myData dataId];
    itemsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:itemsVC animated:YES];
}



#pragma CreateData
- (void)createData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.liwushuo.com/v2/items?gender=1&generation=1&limit=50&offset=0" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary *dic = responseObject;
        self.itemArr = [NSMutableArray array];
        self.itemArr = [HZMHotModel baseModelByArr:dic[@"data"][@"items"]];
        
        self.myData = [[[HZMDataModel alloc] init] autorelease];
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        
    }];
}



- (void) createNetWork {
    AFNetworkReachabilityManager *manager =  [AFNetworkReachabilityManager sharedManager];
    [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        [self showNetWorkWithStatus:status];
        
    }];
}

- (void) showNetWorkWithStatus:(NSInteger)status {
    NSString *str;
    switch (status) {
        case AFNetworkReachabilityStatusUnknown:
            str = @"未知网络";
//            [self.hud removeFromSuperview];
            break;
        case AFNetworkReachabilityStatusNotReachable:
            str = @"网络未连接";
            
//            [self.hud removeFromSuperview];
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            str = @"网络已连接";
            [self createData];
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            str = @"wlfi已连接";
            [self createData];
            break;
        default:
            break;
    }
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前网络状态" message:str preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//    [alert addAction:cancel];
//    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
